package sm.server;


import java.net.ServerSocket;
import java.net.Socket;

import sm.namingservice.ServerState;
import sm.namingservice.ServerTable;
import sm.routine.WorkloadMonitor;
import sm.server.requestOperations.RequestHandler;
import sm.server.vmOperations.LoginAndOut;
import sm.utils.PropertySet;
import sm.utils.ServerUtils;

import com.vmware.vim25.DynamicProperty;
import com.vmware.vim25.ObjectContent;
import com.vmware.vim25.RetrieveResult;

public class Entrya {
	private LoginAndOut _connection;
	private ServerTable _st;
	private ServerState _ss;
	
	public LoginAndOut getLao(){
		return this._connection;
	}
	
	public static void main(String[] args) throws Exception {
		String _hostName = "vmm2.cs.cityu.edu.hk";
		String _user = "cs6282web";
		String _pw = "cs62cs82";
		Entrya entry = new Entrya();
		entry._connection = new LoginAndOut();
		entry._ss = new ServerState();
		entry._st = new ServerTable();
		entry._connection.setConnectAndLogin(_hostName, _user, _pw);
		
		//create socket listen to request
		ServerSocket listenSocket = new ServerSocket(PropertySet._SerMngSocket);
		System.out.println("server Start...");
		
		//start server manager's routines
		WorkloadMonitor wlm = new WorkloadMonitor(entry._st,entry._ss,entry._connection.getMOR(),entry._connection.getLocator(),
				entry._connection.getVimPortType(),entry._connection.getServiceContent());
		
		wlm.start();
		//looping to wait for requests
		while(true){
			//Listen to socket & accept request from clients
			Socket connectionSocket = listenSocket.accept();
			System.out.println("Accepting Connection..."+connectionSocket.getInetAddress());
			
			//create new tread to handle dm & rb connection & requests
			new RequestHandler(connectionSocket,entry._st,entry._ss).start();
		}
		
//		RetrieveResult props= DwsDataRetriever.retrieveData(
//				entry._connection.getServiceContent(), entry._connection.getVimPortType(),
//				new String[]{"VirtualMachine"},"Virtual Lab/vm/cs6282/studentVM",
//				new String[]{"name","runtime.powerState"});
//			
//		for(ObjectContent oc:props.getObjects()){
//			for(DynamicProperty dp:oc.getPropSet()){
//				System.out.println(dp.getName()+"="+dp.getVal());
//			}
//		}
//		ServerUtils.VmOnOffControl(entry._connection.getVimPortType(), props, "51462231_dws_3", PropertySet._powerOn);
//		System.in.read();
//		ServerUtils.VmOnOffControl(entry._connection.getVimPortType(), props, "51462231_dws_3", PropertySet._powerOff);
	}

}
